package com.jia.yygh.hosp.controller;

import com.jia.yygh.common.exception.YyghException;
import com.jia.yygh.common.result.Result;
import com.jia.yygh.common.util.MD5;
import com.jia.yygh.hosp.service.HospitalSetService;
import com.jia.yygh.model.hosp.HospitalSet;
import com.jia.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @GetMapping("/list")
    public Result list(){

        try {
            int res = 1/0;
        }catch (Exception e){
            throw new YyghException("请求异常",201);
        }

        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @GetMapping("info/{id}")
    public Result info(@PathVariable long id){
        HospitalSet info = hospitalSetService.getById(id);
        return Result.ok(info);
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Long id){
        hospitalSetService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/queryPage")
    public Result queryPage(@RequestParam long pageNum, @RequestParam long pageSize, HospitalSetQueryVo hospitalSetQueryVo){
        Map<String,Object> map = hospitalSetService.queryPage(pageNum,pageSize,hospitalSetQueryVo);
        return Result.ok(map);

    }

    @PostMapping("add")
    public Result add(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        // 签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @PutMapping("update")
    public Result update(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<String> idList){
        boolean flag = hospitalSetService.removeByIds(idList);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @PutMapping("lock/{id}/{status}")
    public Result lock(@PathVariable long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        return Result.ok();
    }

    @GetMapping("sendKey/{id}")
    public Result sendKey(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        Map<String,Object> map = new HashMap<>();
        map.put("hoscode",hoscode);
        map.put("signKey",signKey);
        return Result.ok(map);
    }


}
